#!usr/bin/env python
# -*- coding:utf-8 -*-
# user nanme: Mr.zhang
# data1 time : 2022/3/10   5:50 下午
# project   : auto_kika.py
# IDE       : PyCharm
# email     :17600960626@163.com


import logging
import multiprocessing
import os
import uiautomator2 as u2
from time import sleep
from auto_xposed.ui_data import elements
# from public.uninstall import Auto_uninstall
from public.uninstall import Auto_uninstall

XPOSE_APK = 'de.robv.android.xposed.installer' #6.0使用
# XPOSE_APK ='org.meowcat.edxposed.manager' #8.0使用
# ZHU_NAME=zhu_language()
# FU_NAME=fu_language()



class Auto_kika(Auto_uninstall):

    def multi_xpos(self,devices):
        # 多进程启动Xposed
        p = multiprocessing.Pool(len(devices))
        p.map(self.star_xpos60, devices)
        p.close()
        p.join()


    def star_xpos60(self,devices):
        '''开启Xposed对应测试工具和选取条件'''
        d = u2.connect(devices)
        sleep(2)
        d.press("home")
        d.app_start(XPOSE_APK)
        sleep(3)
        d(description="打开导航抽屉").click()
        sleep(1)
        d(text="模块").click()
        sleep(0.5)
        if d.xpath('//*[@text="中文测试工具"]').exists:
            d.xpath('//*[@resource-id="android:id/list"]/android.widget.FrameLayout[2]/android.widget.LinearLayout[1]'
                    '/android.widget.CheckBox[1]').click()
            d.xpath('//*[@resource-id="android:id/list"]/android.widget.FrameLayout[2]').click()
            d.xpath('//*[@resource-id="de.robv.android.xposed.installer:id/title"]').click()
            d.xpath('//*[@resource-id="com.kikatech.hackkeyboardxposedmodule:id/btn_reload"]').click()
            d.xpath('//*[@resource-id="com.kikatech.hackkeyboardxposedmodule:id/button"]').click()
        else:
            d.xpath('//*[@resource-id="de.robv.android.xposed.installer:id/checkbox"]').click()
            d.xpath('//*[@resource-id="de.robv.android.xposed.installer:id/title"]').click()
            d.xpath('//*[@resource-id="com.kikatech.hackkeyboardxposedmodule:id/btn_reload"]').click()
            d.xpath('//*[@resource-id="com.kikatech.hackkeyboardxposedmodule:id/button"]').click()


    def kika_mixed_input(self,sdk_name,devices,zhu_name,fu_name):
        '''
        混输入设置语言
        :param devices: 设备id
        :param zhu_name: 主语料名称
        :param zhu_lang: 主语言sql语言缩写
        :param fu_name: 副语料名称
        :return:
        '''
        for i,j,x in zip(devices,zhu_name,fu_name):
                try:
                    d = u2.connect(i)
                except Exception as e:
                    logging.error('设备获取异常%s'%e)
                    break
                else:
                    #启动app
                    d.app_start(self.apk_dictionary(sdk_name)[0])
                    sleep(1)
                    d(resourceId="com.huawei.ohos.inputmethod:id/rl_join").click()
                    d(resourceId="com.huawei.ohos.inputmethod:id/btn_right").click()
                    sleep(1)
                    d(text="语言").click()
                    sleep(1.5)
                    '''使用共主干海外版设置中文设备语言时开启'''
                    d.xpath(elements.取消中文).click()
                    d(resourceId=elements.语言删除按钮).click()
                    d(resourceId=elements.点击搜索).click()
                    sleep(1.5)
                    d.send_keys(text=x, clear=True)  # x是设置副语言
                    sleep(0.5)
                    d(resourceId="com.huawei.ohos.inputmethod:id/fl_operation").click()
                    d(resourceId="com.huawei.ohos.inputmethod:id/menu_search_view").click()
                    if j == 'English(United States)':
                        pass
                    else:
                        d.send_keys(text=j, clear=True)  # j是设置主语言
                        d.xpath(elements.选择搜索语言).click()
                        d.xpath(elements.复选框1).click()
                        d(resourceId=elements.语言删除按钮).click()
                        d.press('back')
                        d(resourceId=elements.设置输入法为默认).click()
                        d(text="小艺输入法").click()#设置小艺输入法
                    empty = 'adb -s %s shell am start -S com.xinmei365.emptyinput/.MainActivity'%i
                    os.system(empty)
                    sleep(1)
                    d.xpath('//android.widget.EditText').click()
                    sleep(2)
                    os.system('adb -s %s reboot' % i)
                    logging.warning('设备%s 主副语言:%s,%s 等待设备重启……' %(i,j,x))


    def kika_alone_input(self,sdk_name,devices,zhu_name):
        """
        独立输入语言设置
        :param devices: 设备id
        :param zhu_name: 主语料名称
        :param zhu_lang: 主语言sql语言缩写
        """
        for i, j in zip(devices, zhu_name):
            try:
                d = u2.connect(i)
            except Exception as e:
                logging.error('设备获取异常%s' % e)
                break
            else:
                # 启动app
                d.app_start(self.apk_dictionary(sdk_name)[0])
                sleep(1)
                d(resourceId="com.huawei.ohos.inputmethod:id/rl_join").click()
                d(resourceId="com.huawei.ohos.inputmethod:id/btn_right").click()
                sleep(1)
                d(text="语言").click()
                sleep(1.5)
                '''使用共主干海外版设置中文设备语言时开启'''
                d.xpath(elements.取消中文).click()
                d(resourceId=elements.语言删除按钮).click()
                d(resourceId=elements.点击搜索).click()
                sleep(1.5)
                if j == 'English(United States)':
                    pass
                else:
                    d.send_keys(text=j, clear=True)  # j是设置主语言
                    d.xpath(elements.选择搜索语言).click()
                    d.xpath(elements.复选框1).click()
                    d(resourceId=elements.语言删除按钮).click()
                    d.press('back')
                    d(resourceId=elements.设置输入法为默认).click()
                    d(text="小艺输入法").click()  # 设置小艺输入法
                empty = 'adb -s %s shell am start -S com.xinmei365.emptyinput/.MainActivity' % i
                os.system(empty)
                sleep(1)
                d.xpath('//android.widget.EditText').click()
                sleep(2)
                os.system('adb -s %s reboot' % i)
                logging.warning('设备%s 语言:%s 设置完成,等待设备重启……' % (i, j))


    def kika_error_input(self,sdk_name,devices,zhu_name):
        """
        纠错语言设置
        :param sdk_name:
        :param devices:
        :param zhu_name:
        :return:
        """
        for i, j in zip(devices, zhu_name):
            try:
                d = u2.connect(i)
            except Exception as e:
                logging.error('设备获取异常%s' % e)
                break
            else:
                # 启动app
                d.app_start(self.apk_dictionary(sdk_name)[0])
                sleep(1)
                d(resourceId="com.huawei.ohos.inputmethod:id/rl_join").click()
                d(resourceId="com.huawei.ohos.inputmethod:id/btn_right").click()
                sleep(1)
                d(text="语言").click()
                sleep(1.5)
                '''使用共主干海外版设置中文设备语言时开启'''
                d.xpath(elements.取消中文).click()
                d(resourceId=elements.语言删除按钮).click()
                d(resourceId=elements.点击搜索).click()
                sleep(1.5)
                if j == 'English(United States)':
                    pass
                else:
                    d.send_keys(text=j, clear=True)  # j是设置主语言
                    d.xpath(elements.选择搜索语言).click()
                    d.xpath(elements.复选框1).click()
                    d(resourceId=elements.语言删除按钮).click()
                    d.press('back')
                    d(resourceId=elements.设置输入法为默认).click()
                    d(text="小艺输入法").click()  # 设置小艺输入法
                empty = 'adb -s %s shell am start -S com.xinmei365.emptyinput/.MainActivity' % i
                os.system(empty)
                sleep(1)
                d.xpath('//android.widget.EditText').click()
                sleep(2)
                os.system('adb -s %s reboot' % i)
                logging.warning('设备%s 语言:%s 设置完成,等待设备重启……' % (i, j))



    def swk_setting(self,devices,zhu_name,fu_name):
        '''
        swk设置主语言给对应的设备绑定
        :param devices: 设备id
        :param zhu_name: 主语料名称
        :param fu_name: 副语料名称
        :return:
        '''
        for i,j,x in zip(devices,zhu_name,fu_name):
            try:
                d = u2.connect(i)
            except Exception as e:
                logging.error('设备获取异常%s' % e)
                break
            else:
                # 启动app
                d.app_start('com.apkfab.installer')#启动新版xapk安装
                d.xpath(elements.点击install按钮).click()  # 安装第一个xapk包
                d(resourceId=elements.点击xpak安装按钮).click_exists(timeout=10.0)
                d(resourceId=elements.点击done按钮).click_exists(timeout=50.0)
                d.press('home')
                sleep(1)
                d.app_start("com.touchtype.swiftkey",use_monkey=True)
                d(text='启用SwiftKey').click_exists(timeout=50.0)
                d(scrollable=True).scroll(steps=20)
                d(text="Microsoft SwiftKey 键盘").click()
                d(resourceId="android:id/button1").click()
                d(text="选择 SwiftKey").click()
                d(text="Microsoft SwiftKey 键盘").click()
                d(text="完成").click()
                d(resourceId="com.touchtype.swiftkey:id/not_now").click()  # 点击完成
                d(resourceId="com.touchtype.swiftkey:id/positiveButton").click()  # 点击协议确认
                d(resourceId="android:id/button1").click()  # 点击弹窗确认




